package com.dianhun.cn.controller;

import com.dianhun.cn.vo.ChartsVo;
import com.dianhun.cn.vo.FiledVo;
import com.dianhun.cn.vo.LineVo;
import com.dianhun.cn.vo.Product;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

/**
 * @author cyt
 * @create 2020-09-22 15:33
 */
@Slf4j
@CrossOrigin
@RestController
public class TestController {
    @Autowired
    private JestClient client;

    @GetMapping("/getAll/{size}/{num}")
    public List<Product> getAll(@PathVariable("size") int size, @PathVariable("num") int num) throws IOException {
        System.out.println(size + " _ > " + num);
        HashMap<String, Object> map = new HashMap<>();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.size(size);
        searchSourceBuilder.from(num);
        Search search = new Search.Builder(searchSourceBuilder.toString())
                .addIndex("jd")
                .build();

        SearchResult result = client.execute(search);
        List<Product> list = result.getSourceAsObjectList(Product.class, false);
        list.forEach(System.out::println);
        return list;
    }

    @GetMapping("/test")
    public ChartsVo test() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermsAggregationBuilder field = AggregationBuilders.terms("index")
                .field("_index");

        field.subAggregation(AggregationBuilders.terms("content").field("content." + "keyword"));
        searchSourceBuilder.aggregation(field);

        List<String> indexList = Arrays.asList("articles", "dianhun");

        Search search = new Search.Builder(searchSourceBuilder.toString())
                // multiple index or types can be added.
                .addIndices(indexList)
                .addType("test")
                .build();

        SearchResult result = client.execute(search);
        MetricAggregation aggregations = result.getAggregations();

        TermsAggregation index = aggregations.getTermsAggregation("index");
        ArrayList<LineVo> ans = new ArrayList<>();
        HashSet<String> columns = new LinkedHashSet<>();

        LineVo vo;
        ArrayList<FiledVo> voArrayList = new ArrayList<>();
        for (TermsAggregation.Entry bucket : index.getBuckets()) {
            vo = new LineVo();
            vo.setTitle(bucket.getKeyAsString());
            List<TermsAggregation.Entry> content = bucket.getTermsAggregation("content").getBuckets();
            for (TermsAggregation.Entry entry : content) {
                columns.add(entry.getKeyAsString());
                voArrayList.add(new FiledVo(entry.getKey(), entry.getCount()));
            }
            vo.setContent(voArrayList);
            ans.add(vo);
        }

        ChartsVo chartsVo = new ChartsVo("index", columns, ans);
        return chartsVo;
    }
}
